﻿var express = require('express');
var router = express.Router();
var connection = require('../backjs/node_mysql.js');

//设置权限
router.post('/power', function (req, res, next) {
    let sql = 'update user set user_power = ?,user_type = ? where user_id = ?';
    let params = [req.body.user_power, req.body.user_type, req.body.user_id];
    connection.query(sql, params, function (err, rs) {
        if (err) {
            throw err;
        }
        else {
            //如果从员工调整为客户，则需要从员工表staff中删除该条记录
            if (req.body.user_type == '1'){
                let sql = 'delete from staff where staff_id = ?'
                let params = [req.body.user_id]
                connection.query(sql, params, function (err, rs) {
                    if (err) {
                        throw err;
                    }
                    else {
                        res.send(200);
                    }
                });
            }
            if (req.body.user_type == '2') {
                let sql = 'insert into staff (staff_id, staff_update) values (?, 1) on duplicate key update staff_update = values(staff_update)';
                let params = [req.body.user_id];
                connection.query(sql, params, function (err, rs) {
                    if (err) {
                        throw err;
                    }
                    else {
                        res.send(200);
                    }
                });
            }
            else {
                res.send(200);
            }
        }
    });
});

//设置部门
router.post('/department', function (req, res, next) {
    if (req.body.requestType == 'insert') {
        let sql = 'insert into part (part_name,part_m) values (?, ?)';
        let params = [req.body.part_name, req.body.part_m];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    if (req.body.requestType == 'update') {
        let sql = 'update part set part_name = ?,part_m = ? where part_id = ?';
        let params = [req.body.part_name, req.body.part_m, req.body.part_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(200);
            }
        });
    }
    //检测部门列表
    if (req.body.requestType == 'list') {
        let sql = 'select part.*,user_name from part left join user on user_id = part_m';
        connection.query(sql, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
});

//设置小组
router.post('/team', function (req, res, next) {
    if (req.body.requestType == 'insert') {
        let sql = 'insert into team (team_name,team_uid,team_part) values (?, ?, ?)';
        let params = [req.body.team_name, req.body.team_uid,req.body.team_part];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    if (req.body.requestType == 'update') {
        let sql = 'update team set team_name = ?,team_uid = ? where team_id = ?';
        let params = [req.body.team_name, req.body.team_uid, req.body.team_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(200);
            }
        });
    }
    if (req.body.requestType == 'list') {
        let sql = 'select team.*,user_name,part_name from team,user,part where part_id = ? and part_id = team_part and team_uid = user_id order by team_id DESC';
        let params = [req.body.part_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    if (req.body.requestType == 'superManager') {
        let sql = 'select team.*,user_name,part_name from team,user,part where  part_id = team_part and team_uid = user_id order by team_id DESC';
        connection.query(sql, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
});

//分配检测员工
router.post('/staff', function (req, res, next) {
    if (req.body.requestType == 'insert') {
        let sql = 'insert into staff (staff_id) values (?)';
        let params = [req.body.staff_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(200);
            }
        });
    }
    if (req.body.requestType == 'update') {
        let sql = 'update staff set staff_part = ?,staff_team = ? where staff_id in (?)';
        let params = [req.body.staff_part, req.body.staff_team == "" ? null : req.body.staff_team, req.body.staff_id];
        console.log(req.body);
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(200);
            }
        });
    }
    //所有员工列表
    if(req.body.requestType == 'allList'){
        let sql = 'select staff.*,user_name, user_account,part_name,team_name from staff left join user on user_id = staff_id left join part ' +
            'on part_id = staff_part left join team on team_id = staff_team';
        connection.query(sql, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    //本部门员工列表
    if (req.body.requestType == 'partList') {
        let sql = 'select tmp.*,user_name,user_account,part_name,team_name from (select * from staff where staff_part = ?)tmp left join user on user_id = staff_id left join part ' +
            'on part_id = staff_part left join team on team_id = staff_team';
        let params = [req.body.user_part];
        connection.query(sql, params,function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    //小组员工列表
    if (req.body.requestType == 'teamList') {
        let sql = 'select tmp.*,user_name,user_account,part_name,team_name from (select * from staff where staff_team = ?)tmp left join user on user_id = staff_id left join part ' +
            'on part_id = staff_part left join team on team_id = staff_team';
        let params = [req.body.user_team];
        connection.query(sql, params,function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
});

//客户列表
router.post('/client', function (req, res, next) {
    let sql = 'select user_id,user_name,user_company from user where user_type = 1';
    connection.query(sql, function (err, rs) {
        if (err) {
            throw err;
        }
        else {
            res.send(rs);
        }
    });
});

router.post('/userList', function (req, res, next) {
    let sql = 'select * from user';
    connection.query(sql, function (err, rs) {
        if (err) {
            throw err;
        }
        else {
            res.send(rs);
        }
    });
});

//姓名查询
router.post('/username', function (req, res, next) {
    let sql = 'select user_name from user where user_id = ?';
    let params = [req.body.user_id];
    connection.query(sql, params, function (err, rs) {
        if (err) {
            throw err;
        }
        else {
            res.send(rs);
        }
    });
});

module.exports = router;